<template>
  <div id="main">
    <h2>职业发展</h2>
    <div class="head">
      <el-input v-model="searchTableInfo" placeholder="请输入搜索内容" style="width:240px;float: right;margin:20px;" />
    </div>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column
          label="序号"
          type="index"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="staffName"
          label="员工姓名"
        />

        <el-table-column
          align="center"
          prop="staffHiredate"
          label="入职时间"
        />

        <el-table-column
          align="center"
          prop="staffDuty"
          label="职务"
        />

        <el-table-column
          align="center"
          prop="staffTitle"
          label="职称"
        />

        <el-table-column
          align="center"
          prop="staffRank"
          label="职级"
        />

        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleOpen(Scope.row.staffId)">编辑</el-button>
            <!-- <el-button type="danger" icon="el-icon-delete" @click="handleDelete(Scope.row.staffId, Scope.row.staffName)">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5, 10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog title="修改职业发展信息" :close-on-click-modal="false" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form ref="staff" :model="staff" label-position="top" :rules="rules">

        <!-- <el-form-item prop="staffName" label="员工姓名" :label-width="formLabelWidth"> -->
        <!-- <el-input maxlength="20" v-model="staff.staffName" autocomplete="off" readonly /> -->
        <!-- </el-form-item> -->
        <el-form-item
          prop="staffName"
          label="员工姓名"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffName"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item prop="staffGraduationtime" label="毕业时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="staff.staffGraduationtime"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item
          prop="staffJobresume"
          label="工作履历"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffJobresume"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>
        <!-- <el-form-item prop="staffJobresume" label="工作履历" :label-width="formLabelWidth">
          <el-input v-model="staff.staffJobresume" autocomplete="off" maxlength="20" show-word-limit />
        </el-form-item> -->
        <el-form-item prop="staffHiredate" label="入职时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="staff.staffHiredate"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item
          prop="staffDuty"
          label="职务"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffDuty"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="staffTitle"
          label="职称"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffTitle"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="staffRank"
          label="职级"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffRank"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="staffProfession"
          label="专业特长"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffProfession"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="staffTraining"
          label="培训履历"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffTraining"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="staffWorksummary"
          label="工作总结"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffWorksummary"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="staffDevelop"
          label="专业发展"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="staff.staffDevelop"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleUpdate('staff')">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { getAllStaff, getStaffById, updateStaff } from '@/api/staff'
export default {
// import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    // 这里存放数据
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      tableList: [],
      listLoading: false,
      dialogFormVisible: false,
      staff: {},
      newStaff: { },
      formLabelWidth: '200px',
      searchTableInfo: '',
      rules: {}
    }
  },
  // 监听属性 类似于data概念
  computed: {
    list() {
      const searchTableInfo = this.searchTableInfo
      if (searchTableInfo) {
        return this.tableList.filter(data => {
          return Object.keys(data).some(key => {
            return (
              String(data[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            )
          })
        })
      }
      return this.tableList
    }
  },
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.tableList = []
      this.listLoading = true
      getAllStaff().then(data => {
        for (var i = 0; i < data.obj.length; i++) {
          this.newStaff.staffId = data.obj[i].staffId
          this.newStaff.staffName = data.obj[i].staffName
          this.newStaff.staffHiredate = data.obj[i].staffHiredate
          this.newStaff.staffDuty = data.obj[i].staffDuty
          this.newStaff.staffTitle = data.obj[i].staffTitle
          this.newStaff.staffRank = data.obj[i].staffRank
          this.tableList.push(this.newStaff)
          this.newStaff = {}
        }
        this.listLoading = false
      })
    },
    handleOpen(id) {
      this.staff = {}
      this.dialogFormVisible = true
      getStaffById(id).then(resp => {
        this.staff = resp.obj
      })
    },
    handleClose() {
      this.dialogFormVisible = false
      this.staff = {}
    },
    handleUpdate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.staff)
          updateStaff(this.staff).then(resp => {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    }
  } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
</style>
